#include "OiPCH.hpp"

namespace Oi
{
    namespace PLScannerCore
    {

        ResultSplineView::ResultSplineView(QStringList resultItems, QWidget *parent /*= Q_NULLPTR*/)
            : DynamicSplineView(resultItems, parent)
        {
            chart()->setMinYRange(0.0, 2.0);
        }

        ResultSplineView::~ResultSplineView()
        {

        }

        QMap<QString, QList<QPointF>> ResultSplineView::loadData(const QDateTime from, const QDateTime end)
        {
            QStringList splList = splines();

            QMap<QString, QList<QPointF>> retPoints;
            for(auto spline : splList)
            {
                retPoints[spline] = QList<QPointF>();
            }

            
            QString mformular = formular();

            if (mformular.isEmpty())
            {
                return retPoints;
            }

            QMap<qreal, QMap<QString, QVariantMap>> datas;
            QMap<qreal, int> ids;

            Database::instance()->query(datas, ids, mformular, from, end);

            for (auto& di = datas.begin(); di != datas.end(); di++)
            {
                auto dv = di.value();
                for (auto spline : splList)
                {
                    if(dv.contains(spline) && dv[spline].contains("Value"))
                    {
                        retPoints[spline].append(QPointF(di.key(), dv[spline]["Value"].toDouble()));
                    }
                }
            }
            
            return retPoints;
        }

        void ResultSplineView::addData(const qreal dt, QMap<QString, qreal> data)
        {
            chart()->addValues(dt, data);
        }
    }
}